package LOGIN;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.xfengshopping.R;
import com.google.gson.Gson;
import com.google.gson.JsonObject;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;

public class register extends AppCompatActivity {
    private EditText accountRegister;
    private EditText passwordRegister;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);
        backListener();
        backLoginListener();
        registerListener();
        accountRegister = findViewById(R.id.account_register);
        passwordRegister = findViewById(R.id.Password_register);
    }

    private void backListener() {
        Button back = findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(register.this, login.class);
                startActivity(intent);
                finish();
            }
        });
    }

    private void backLoginListener() {
        ImageView backLogin = findViewById(R.id.backLogin);
        backLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(register.this, login.class);
                startActivity(intent);
                finish();
            }
        });
    }

    private void registerListener() {
        Button registerButton = findViewById(R.id.register);
        registerButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String account = accountRegister.getText().toString();
                String password = passwordRegister.getText().toString();
                if (!account.isEmpty() && !password.isEmpty()) {
                    registerUser(account, password);
                } else {
                    Toast.makeText(register.this, "请输入账户和密码", Toast.LENGTH_LONG).show();
                }
            }
        });
    }

    private void registerUser(String account, String password) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    // 构建请求体
                    JsonObject requestBody = new JsonObject();
                    requestBody.addProperty("loginName", account);
                    requestBody.addProperty("password", password);

                    // 构建请求URL
                    URL url = new URL("http://47.99.134.126:28019/api/v1/user/register");
                    //URL url = new URL("http://115.158.64.84:28019/api/v1/user/register");
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setRequestMethod("POST");
                    connection.setRequestProperty("Content-Type", "application/json; charset=utf-8");
                    connection.setRequestProperty("Accept", "application/json");
                    connection.setDoOutput(true);
                    connection.setDoInput(true);

                    // 发送请求体
                    try (DataOutputStream outputStream = new DataOutputStream(connection.getOutputStream())) {
                        outputStream.write(new Gson().toJson(requestBody).getBytes(StandardCharsets.UTF_8));
                        outputStream.flush();
                    }

                    // 获取响应
                    int responseCode = connection.getResponseCode();
                    if (responseCode == HttpURLConnection.HTTP_OK) {
                        try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) {
                            StringBuilder response = new StringBuilder();
                            String line;
                            while ((line = reader.readLine()) != null) {
                                response.append(line);
                            }
                            final String responseBody = response.toString();
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(register.this, "注册成功: " + responseBody, Toast.LENGTH_LONG).show();
                                }
                            });
                        }
                    } else {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(register.this, "注册失败，响应码: " + responseCode, Toast.LENGTH_LONG).show();
                            }
                        });
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(register.this, "网络错误: " + e.getMessage(), Toast.LENGTH_LONG).show();
                        }
                    });
                }
            }
        }).start();
    }
}
